package com.glinsoft.ticktak.controller;

import com.glinsoft.ticktak.controller.common.JSON;
import com.glinsoft.ticktak.controller.common.WebConsts;
import com.glinsoft.ticktak.service.ClusterService;
import com.glinsoft.ticktak.vo.ClusterVo;

import com.glinsoft.waterbatis.query.Page;

import io.swagger.annotations.*;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.*;


@Api(tags = "集群配置")
@Slf4j
@RestController
@RequestMapping("/cluster")
public class ClusterController {
    @Autowired
    private ClusterService service;

    /**
     * 获取指定ID的对象
     * @param id 数据库主键
     * @return 返回指定ID的对象，不存在返回null
     */
    @ApiOperation(value = "查询", notes = "根据主键查询单条记录")
    @GetMapping(value = "/get", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON<ClusterVo> getById(String id) {
        log.debug("====> 集群配置(Cluster)根据主键查询，参数：{}", id);

        ClusterVo vo = service.get(id);

        return JSON.OK(vo);
    }

    /**
     * 新增记录
     * @param model 需要新增对象及属性值
     * @return 返回处理结果
     */
    @ApiOperation(value = "新增", notes = "新增记录")
    @PostMapping(value = "/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON<String> add(ClusterVo model) {
        log.debug("====> 集群配置(Cluster)新增记录");

        Object id = service.save(model);

        return JSON.OK("已完成新增条目，主键：" + id);
    }

    /**
     * 根据ID删除记录
     * @param id 需要删除记录的主键
     * @return 返回处理结果
     */
    @ApiOperation(value = "删除", notes = "删除指定主键的记录")
    @PostMapping(value = "/delete", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON<String> delete(String id) {
        log.debug("====> 集群配置(Cluster)删除记录，参数：{}", id);
        service.delete(id);

        return JSON.OK("记录已删除");
    }

    /**
     * 根据ID更新记录数据
     * @param model 需要更新对象及属性值
     * @return 返回处理结果
     */
    @ApiOperation(value = "更新", notes = "根据主键更新数据记录")
    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON<String> update(ClusterVo model) {
        log.debug("====> 集群配置(Cluster)更新记录");
        service.update(model);

        return JSON.OK("记录已完成更新");
    }

    /**
     * 查询指定页码的数据
     * @param pageIndex 当前页码
     * @param pageSize 每页显示记录数
     * @return 返回指定页数据
     */
    @ApiOperation(value = "分页查询", notes = "查询指定页码的数据")
    @GetMapping(value = "/list", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSON<Page<ClusterVo>> listPage(Integer pageIndex, Integer pageSize) {
        log.debug("====> 集群配置(Cluster)分页查询，参数：{},{}", pageIndex, pageSize);

        if ((pageIndex == null) || (pageIndex < 1)) {
            pageIndex = 1;
        }

        if ((pageSize == null) || (pageSize < 1)) {
            pageSize = WebConsts.PAGE_SIZE;
        }

        return JSON.OK(service.listPage(pageIndex, pageSize));
    }
}
